<template>
  <el-dialog id="edit-dialog" :title="formValidate.id ? $t('channel.btn.edit') : $t('channel.btn.add')" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item :label="$t('channel.tabData.channelProvider')" prop="name">
        <el-input v-model="formValidate.name" clearable :placeholder="$t('channel.prompt.channelProvider')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('channel.tabData.channelNumber')" prop="serialNumber">
        <el-input v-model="formValidate.serialNumber" clearable :placeholder="$t('channel.prompt.Id')" :maxlength="2" :disabled="!!formValidate.id" @input="test" />
      </el-form-item>
      <el-form-item :label="$t('channel.tabData.channelStatus')" prop="state">
        <el-select v-model="formValidate.state" :placeholder="$t('common.select')" style="width: 100%">
          <el-option
            v-for="item in $t('channel.channelStatus')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addChannel, updateChannel } from '@/api/operation'
export default {
  name: 'ChannelListDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        name: null,
        serialNumber: null,
        state: null
      },
      rules: {
        name: [{ required: true, message: this.$t('channel.tabData.channelProvider') + this.$t('common.null'), trigger: 'blur' }],
        serialNumber: [{ required: true, message: this.$t('channel.tabData.channelNumber') + this.$t('common.null'), trigger: 'blur' }],
        state: [{ required: true, message: this.$t('channel.tabData.channelStatus') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    test() {
      let value = this.searchForm.serialNumber
      if (value.length > 0) {
        value = value.replace(/[^A-Z0-9]/g, '')
      }
      this.searchForm.serialNumber = value
    },
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          const data = this.formValidate
          if (data.id) {
            updateChannel(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addChannel(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
